package com.ss.android.article.base.ui;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.bytedance.common.utility.UIUtils;

public class DrawableButton extends View {
    private int A;
    private Drawable a;
    private Drawable b;
    private Drawable c;
    private Drawable d;
    private String e;
    private int f;
    private int g;
    private ColorStateList h;
    private int i;
    private int j;
    private int k;
    private Context l;
    private Paint m;
    private Paint.FontMetricsInt n;
    private int o;
    private String p;
    private int q;
    private int r;
    private int s;
    private int t;
    private int u;
    private int v;
    private boolean w;
    private int x;
    private int y;
    private int z;

    public DrawableButton(Context arg2) {
        super(arg2);
        this.f = 17;
        this.l = arg2;
        this.b();
    }

    public DrawableButton(Context arg2, AttributeSet arg3) {
        super(arg2, arg3);
        this.f = 17;
        this.l = arg2;
        this.a(arg3);
        this.b();
    }

    public DrawableButton(Context arg2, AttributeSet arg3, int arg4) {
        super(arg2, arg3, arg4);
        this.f = 17;
        this.l = arg2;
        this.a(arg3);
        this.b();
    }

    @TargetApi(value = 21)
    public DrawableButton(Context arg2, AttributeSet arg3, int arg4, int arg5) {
        super(arg2, arg3, arg4, arg5);
        this.f = 17;
        this.l = arg2;
        this.a(arg3);
        this.b();
    }

    public final void setText(String arg2, boolean arg3) {
        if (!android.support.design.a.isTextEquals(arg2, this.e)) {
            this.e = arg2;
            boolean v0 = this.e == null || this.e.length() == 0 ? true : false;
            this.w = v0;
            if (!arg3) {
                return;
            }
            this.requestLayout();
        }
    }

    public final void setDrawable(Drawable drawable, boolean requestLayout) {
        if (drawable != this.a) {
            if (this.a != null) {
                this.a.setCallback(null);
            }

            this.a = drawable;
            if (this.a != null) {
                this.a.setBounds(0, 0, this.a.getIntrinsicWidth(), this.a.getIntrinsicHeight());
                this.a.setCallback(this);
                this.a.setState(this.getDrawableState());
            }

            if (requestLayout) {
                this.requestLayout();
            }
        }
    }

    public final void a(int arg2, boolean arg3) {
        if (arg2 != this.j) {
            this.j = arg2;
            if (arg3) {
                this.requestLayout();
            }
        }
    }

    public final void a(ColorStateList arg3, boolean arg4) {
        if (arg3 != this.h) {
            this.h = arg3;
            if (this.h != null) {
                this.m.setColor(this.h.getDefaultColor());
            }

            if (!arg4) {
                return;
            }

            this.invalidate();
        }
    }

    public final void a() {
        if (17 != this.f) {
            this.f = 17;
        }
    }

    private void a(AttributeSet arg8) {
        TypedArray v3 = this.l.obtainStyledAttributes(arg8, com.ss.android.article.base.a.w, 0, 0);
        int v4 = v3.getIndexCount();
        int v2;
        for (v2 = 0; v2 < v4; ++v2) {
            int v0 = v3.getIndex(v2);
            if (v0 == com.ss.android.article.base.a.y) {
                this.a = v3.getDrawable(v0);
                this.a.setBounds(0, 0, this.a.getIntrinsicWidth(), this.a.getIntrinsicHeight());
                this.a.setCallback(((Drawable.Callback) this));
            } else if (v0 == com.ss.android.article.base.a.A) {
                this.b = v3.getDrawable(v0);
                this.b.setBounds(0, 0, this.b.getIntrinsicWidth(), this.b.getIntrinsicHeight());
                this.b.setCallback(((Drawable.Callback) this));
            } else if (v0 == com.ss.android.article.base.a.B) {
                this.c = v3.getDrawable(v0);
                this.c.setBounds(0, 0, this.c.getIntrinsicWidth(), this.c.getIntrinsicHeight());
                this.c.setCallback(((Drawable.Callback) this));
            } else if (v0 == com.ss.android.article.base.a.x) {
                this.d = v3.getDrawable(v0);
                this.d.setBounds(0, 0, this.d.getIntrinsicWidth(), this.d.getIntrinsicHeight());
                this.d.setCallback(((Drawable.Callback) this));
            } else if (v0 == com.ss.android.article.base.a.E) {
                this.e = (String) v3.getText(v0);
                boolean v0_1 = this.e == null || this.e.length() == 0 ? true : false;
                this.w = v0_1;
            } else {
                if (v0 == com.ss.android.article.base.a.z) {
                    this.g = v3.getDimensionPixelSize(v0, 0);
                } else if (v0 == com.ss.android.article.base.a.F) {
                    this.h = v3.getColorStateList(v0);
                } else if (v0 == com.ss.android.article.base.a.G) {
                    this.i = v3.getDimensionPixelSize(v0, 24);
                } else if (v0 == com.ss.android.article.base.a.D) {
                    this.j = v3.getDimensionPixelSize(v0, 0);
                } else if (v0 != com.ss.android.article.base.a.C) {
                } else {
                    this.k = v3.getDimensionPixelSize(v0, 0);
                }
            }
        }

        v3.recycle();
    }

    private void b() {
        this.setWillNotDraw(false);
        this.m = new Paint(1);
        this.m.setTextSize(((float) this.i));
        if (this.h != null) {
            this.m.setColor(this.h.getDefaultColor());
        }

        this.n = this.m.getFontMetricsInt();
        this.o = UIUtils.a(this.m.measureText("…"));
    }

    protected void drawableStateChanged() {
        super.drawableStateChanged();
        int[] v0 = this.getDrawableState();
        if (v0 != null) {
            if (this.a != null && (this.a.isStateful())) {
                this.a.setState(v0);
            }

            if (this.b != null && (this.b.isStateful())) {
                this.b.setState(v0);
            }

            if (this.c != null && (this.c.isStateful())) {
                this.c.setState(v0);
            }

            if (this.d == null) {
                return;
            }

            if (!this.d.isStateful()) {
                return;
            }

            this.d.setState(v0);
        }
    }

    public Drawable getDrawableBottom() {
        return this.d;
    }

    public Drawable getDrawableLeft() {
        return this.a;
    }

    public int getDrawablePadding() {
        return this.g;
    }

    public Drawable getDrawableRight() {
        return this.b;
    }

    public Drawable getDrawableTop() {
        return this.c;
    }

    public int getGravity() {
        return this.f;
    }

    public int getMinHeight() {
        return this.k;
    }

    public int getMinWidth() {
        return this.j;
    }

    public String getText() {
        return this.e;
    }

    public ColorStateList getTextColor() {
        return this.h;
    }

    public int getTextSize() {
        return this.i;
    }

    protected void onDraw(Canvas arg6) {
        super.onDraw(arg6);
        arg6.save();
        arg6.translate(((float) this.x), ((float) this.y));
        if (this.a != null) {
            arg6.save();
            arg6.translate(0f, ((float) (this.z + (this.v - this.a.getIntrinsicHeight()) / 2)));
            this.a.draw(arg6);
            arg6.restore();
        }

        if (this.b != null) {
            arg6.save();
            arg6.translate(((float) (this.q - this.b.getIntrinsicWidth())), ((float) (this.z + (this.v - this.b.getIntrinsicHeight()) / 2)));
            this.b.draw(arg6);
            arg6.restore();
        }

        if (this.c != null) {
            arg6.save();
            arg6.translate(((float) (this.A + (this.u - this.c.getIntrinsicWidth()) / 2)), 0f);
            this.c.draw(arg6);
            arg6.restore();
        }

        if (this.d != null) {
            arg6.save();
            arg6.translate(((float) (this.A + (this.u - this.d.getIntrinsicWidth()) / 2)), ((float) (this.r - this.d.getIntrinsicHeight())));
            this.d.draw(arg6);
            arg6.restore();
        }

        if (this.p != null) {
            arg6.drawText(this.p, ((float) (this.A + (this.u - this.s) / 2)), ((float) (this.z + (this.v - this.t) / 2 - this.n.ascent)), this.m);
        }

        arg6.restore();
    }

    protected void onLayout(boolean arg6, int arg7, int arg8, int arg9, int arg10) {
        super.onLayout(arg6, arg7, arg8, arg9, arg10);
        int v0 = arg9 - arg7;
        int v1 = arg10 - arg8;
        if ((this.f & 3) == 3) {
            label_68:
            this.x = this.getPaddingLeft();
        } else if ((this.f & 5) == 5) {
            this.x = v0 - this.q - this.getPaddingRight();
        } else if ((this.f & 1) == 1) {
            this.x = (v0 - this.q) / 2;
        } else {
            this.x = this.getPaddingLeft();
        }

        if ((this.f & 48) == 48) {
            label_80:
            this.y = this.getPaddingTop();
        } else if ((this.f & 80) == 80) {
            this.y = v1 - this.r - this.getPaddingBottom();
        } else if ((this.f & 16) == 16) {
            this.y = (v1 - this.r) / 2;
        } else {
            this.y = this.getPaddingTop();
        }

        this.z = 0;
        if (this.c != null) {
            this.z += this.c.getIntrinsicHeight();
            if (!this.w) {
                this.z += this.g;
            }
        }

        this.A = 0;
        if (this.a != null) {
            this.A += this.a.getIntrinsicHeight();
            if (!this.w) {
                this.A += this.g;
            }
        }
    }

    protected void onMeasure(int arg7, int arg8) {
        int v0 = View.MeasureSpec.getSize(arg7);
        if (View.MeasureSpec.getMode(arg7) == MeasureSpec.UNSPECIFIED) {
            v0 = 2147483647;
        }

        int v1 = this.getPaddingLeft() + this.getPaddingRight();
        if (this.a != null) {
            if (!this.w) {
                v1 += this.g;
            }

            v1 += this.a.getIntrinsicWidth();
        }

        if (this.b != null) {
            if (!this.w) {
                v1 += this.g;
            }

            v1 += this.b.getIntrinsicWidth();
        }

        UIUtils.a(this.e, v0 - v1, this.m, this.o, UIUtils.a);
        this.p = UIUtils.a.a;
        v0 = UIUtils.a.b;
        this.s = v0;
        if (this.c != null) {
            v0 = Math.max(v0, this.c.getIntrinsicWidth());
        }

        if (this.d != null) {
            v0 = Math.max(v0, this.d.getIntrinsicWidth());
        }

        int v2 = v1 + v0;
        this.u = v0;
        this.q = v2 - this.getPaddingLeft() - this.getPaddingRight();
        v0 = this.getPaddingTop() + this.getPaddingBottom();
        if (this.c != null) {
            if (!this.w) {
                v0 += this.g;
            }

            v0 += this.c.getIntrinsicWidth();
        }

        if (this.d != null) {
            if (!this.w) {
                v0 += this.g;
            }

            v0 += this.d.getIntrinsicWidth();
        }

        v1 = this.n.descent - this.n.ascent;
        this.t = v1;
        if (this.a != null) {
            v1 = Math.max(v1, this.a.getIntrinsicHeight());
        }

        if (this.b != null) {
            v1 = Math.max(v1, this.b.getIntrinsicHeight());
        }

        v0 += v1;
        this.v = v1;
        this.r = v0 - this.getPaddingBottom() - this.getPaddingTop();
        this.setMeasuredDimension(Math.max(this.j, DrawableButton.resolveSize(v2, arg7)), Math.max(this.k, DrawableButton.resolveSize(v0, arg8)));
    }

    protected void onSizeChanged(int arg3, int arg4, int arg5, int arg6) {
        super.onSizeChanged(arg3, arg4, arg5, arg6);
        Drawable v0 = this.getBackground();
        if (v0 != null) {
            v0.setBounds(0, 0, arg3, arg4);
        }
    }

    public final void setDrawableTop$4eb43225(Drawable arg5) {
        if (this.c != arg5) {
            if (this.c != null) {
                this.c.setCallback(null);
            }

            this.c = arg5;
            if (this.c != null) {
                this.c.setBounds(0, 0, this.c.getIntrinsicWidth(), this.c.getIntrinsicHeight());
                this.c.setCallback(((Drawable.Callback) this));
                this.c.setState(this.getDrawableState());
            }

            this.requestLayout();
        }
    }

    protected boolean verifyDrawable(Drawable arg2) {
        boolean v0 = arg2 == this.a || arg2 == this.b || arg2 == this.c || arg2 == this.d ? true : super.verifyDrawable(arg2);
        return v0;
    }
}

